#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "link_puc.h"

void show_datas_sub(Subjects *subs){
    if(subs == NULL){
        printf("%c没有课程信息\n", eg);
        return;
    }
    printf("%c%s课程信息%s\n", eg, ty, ty);
    printf("%c%s\t%-15s\t%s\t%s\t%s\t%s\n", \
               eg, "编号", "课程名", "性质", \
               "课时", "学分", "学生人数");

    while(subs != NULL){
        Subject sub = subs->sub;
        printf("%c%3d\t%-15s\t%-3s\t%3d\t%.1f\t%2d/%-2d\t\n", \
               eg, sub.id, sub.name, sub.nature, \
               sub.period, sub.credit, sub.num, sub.max_num);
        subs = subs->next;
    }
    free_subs(subs);
}

void add_data_sub(){
    printf("%c%s添加信息%s\n", eg, ty, ty);
    int id, period, max_num;
    char *name = (char*) malloc(10*sizeof(char));
    char *nature = (char*) malloc(10*sizeof(char));
    float credit;
    printf("%c输入课程编号：", eg);
    scanf("%d", &id);
    getchar();
    printf("%c输入课程名称：", eg);
    scanf("%s", name);
    getchar();
    printf("%c输入课程性质：", eg);
    scanf("%s", nature);
    getchar();
    printf("%c输入授课学时：", eg);
    scanf("%d", &period);
    printf("%c输入课程学分：", eg);
    scanf("%f", &credit);
    printf("%c输入课程人数上限：", eg);
    scanf("%d", &max_num);
    getchar();
    Subject sub;
    sub.id = id;
    sub.name = name;
    sub.num = 0;
    sub.credit = credit;
    sub.nature = nature;
    sub.period = period;
    sub.max_num = max_num;
    BackCall call = put_sub(sub);
    if(call.code == 1){
        printf("%c添加成功\n", eg);
    }else{
        printf("%c%s\n", eg, call.msg);
    }
    free_sub(sub);
}

void delete_data_sub(){
    printf("%c%s删除信息%s\n", eg, ty, ty);
    printf("%c输入需要删除课程的编号：", eg);
    int id;
    scanf("%d", &id);
    getchar();
    BackCall call = delete_sub_id(id);
    if (call.code == 1){
        printf("%c删除成功\n", eg);
    }else{
        printf("%c%s\n", eg, call.msg);
    }
}

void update_date_sub(){
    printf("%c%s修改信息%s\n", eg, ty, ty);
    printf("%c输入需要修改课程的编号：", eg);
    int id;
    scanf("%d", &id);
    getchar();
    BackCall call = get_sub_id(id);
    if (call.code != 1 || call.stus == NULL){
        printf("%c找不到该学号的信息\n", eg);
        return;
    }
    Subject s1 = call.subs->sub;
    int period, max_num;
    char *name = (char*) malloc(10*sizeof(char));
    char *nature = (char*) malloc(10*sizeof(char));
    float credit;
    
    int flag = 1;
    char buff[1024];
    printf("%c课程编号(%d)\n", eg, id);
    printf("%c课程名称(%s)\n", eg, s1.name);
    strcpy(name, s1.name);
    printf("%c课程性质(%s)\n", eg, s1.nature);
    strcpy(nature, s1.nature);
    printf("%c输入授课学时(%d):", eg, s1.period);
    int i = 0;
    while((buff[i++] = getchar())!= '\n'){}
    if (buff[0] < '0' || buff[0] > '9'){
        period = s1.period;
    }else{
        flag = 0;
        period = 0;
        i = 0;
        while(buff[i] >= '0' && buff[i] <= '9'){
            period = period * 10 + (buff[i] - '0');
            i++;
        }
    }
    credit = period/16.0;
    printf("%c课程学分(%.1f)\n", eg, credit);
    printf("%c输入课程人数上限(%d):", eg, s1.max_num);
    i = 0;
    while((buff[i++] = getchar())!= '\n'){}
    if (buff[0] < '0' || buff[0] > '9'){
        max_num = s1.max_num;
    }else{
        flag = 0;
        i = 0;
        max_num = 0;
        while(buff[i] >= '0' && buff[i] <= '9'){
            max_num = max_num * 10 + (buff[i] - '0');
            i++;
        }
    }

    if (flag){
        printf("%c你未做任何更改\n", eg);
        return;
    }

    Subject s2;
    s2.id = id;
    s2.name = name;
    s2.credit = credit;
    s2.nature = nature;
    s2.period = period;
    s2.max_num = max_num;
    call = update_sub(s2);
    if(call.code == 1){
        printf("%c更改成功\n", eg);
    }else{
        printf("%c%s\n", eg, call.msg);
    }
    free_sub(s1);
    free_sub(s2);
}

void show_data_sub(){
    printf("%c%s详细信息%s\n", eg, ty, ty);
    printf("%c输入课程编号：", eg);
    int id;
    scanf("%d", &id);
    getchar();
    BackCall call = get_sub_id(id);
    if(call.code != 1){
        printf("%c没有找到该课程信息\n", eg);
        return;
    }
    Subject sub = call.subs->sub;
    printf("%c%s\t%-15s\t%s\t%s\t%s\t%s\n", \
               eg, "编号", "课程名", "性质", \
               "课时", "学分", "学生人数");
    printf("%c%3d\t%-15s\t%-3s\t%3d\t%.1f\t%2d/%-2d\t\n", \
               eg, sub.id, sub.name, sub.nature, \
               sub.period, sub.credit, sub.num, sub.max_num);
    call = get_stu_by_sub(sub);
    if(call.code != 1){
        printf("%c该课程还未有学生选择\n", eg);
        return;
    }
    printf("该课程学生如下：\n");
    Students *stus = call.stus;
    printf("%c学号\t姓名\n", eg);
    while(stus != NULL){
        Student s = stus->stu;
        printf("%c%3d\t%-3s\n", eg, s.id, s.name);
        stus = stus->next;
    }
    free_sub(sub);
    free_stus(stus);
}

void show_init_sub(){
    printf("%c添加课程信息(1)\n", eg);
    printf("%c删除课程信息(2)\n", eg);
    printf("%c修改课程信息(3)\n", eg);
    printf("%c查看课程详细信息(4)\n", eg);
    printf("%c查看所有课程信息(5)\n", eg);
    printf("%c退出(0)：\n", eg);
    printf("%c请选择操作(0-5)：", eg);
}

int show_sub(Subjects *subs){
    printf("%c%s课程管理界面%s\n", eg, ty, ty);
    if(subs != NULL){
        show_datas_sub(subs);
    }
    int type;
    do{
        show_init_sub();
        scanf("%d", &type);
        getchar();
        if(type == 0){
            break;
        }
        switch(type){
        case 1: add_data_sub();
            break;
        case 2: delete_data_sub();
            break;
        case 3: update_date_sub();
            break;
        case 4: show_data_sub();
            break;
        case 5: show_datas_sub(get_sub_all().subs);
            break;
        }

    }while(1);
    return 0;
}
